require "arknights/utils"

local first_tag = {x=1600, y=220, c=0x313131}
local first_tag_active = {x=1600, y=220, c=0x98dc}
local collect_all_tag = {x=1765, y=220, c=0xffffff}

local weekly_tabs = {
    {x=1405, y=60, c=0x313131},
    {x=1298, y=87, c=0x98dc}
}
local all_done_hint = {
    {x=160, y=256, c=0xe1e1e1},
    {x=155, y=256, c=0x313131},
    {x=145, y=256, c=0xb1b2b1}
}
local mission_home = {x=1328, y=885, c=0xe66229}
local shortcut_button = {x=400, y=55, c=0xD8D8D8}
local mission_shortcut = {x=1630, y=60, c=0xffffff}

local auto_mission = {}

-- return true when it is time to do this task
auto_mission.engage = function()
    if qcIsColor(mission_home) then
        waitForColor(shortcut_button, 500, 
            function() 
                qcRandTap(mission_home, 10, 1000)
            end
        )
        return true
    elseif qcIsColor(shortcut_button) then
        waitForColor(mission_shortcut, 400, function()
            qcRandTap(shortcut_button, 10)
        end
        )

        waitWhileColor(mission_shortcut, 400, function()
            qcRandTap(mission_shortcut, 10, 500)
        end
        )

        waitForColor(shortcut_button, 500)
        return true
    else
        return false
    end
end

auto_mission.run = function()
    while not (qcIsColor(first_tag) or qcAllIsColor(all_done_hint)) do
        waitWhileColor(collect_all_tag, 400,
            function()
                qcRandTap(collect_all_tag, 10, 1500)
            end
        )
        waitForColor(shortcut_button, 300, 
            function()
                qcRandTap({x=960, y=1000}, 10, 450)
            end
        )
    end

    waitForAllColors(weekly_tabs, 500, 
        function()
            qcRandTap(weekly_tabs[1],15,400)
        end
    )
    
    while not (qcIsColor(first_tag) or qcAllIsColor(all_done_hint)) do
        waitWhileColor(collect_all_tag, 400,
            function()
                qcRandTap(collect_all_tag, 10, 1500)
            end
        )
        waitForColor(shortcut_button, 300, 
            function()
                qcRandTap({x=960, y=1000}, 10, 450)
            end
        )
    end
end


if __name__ == "main" then
    return auto_mission
else
    init(1)
    auto_mission.run()
end